package com.by;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

public class Productor {
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        //1.创建连接工厂   创建连接
        ConnectionFactory factory = new ConnectionFactory();
        factory.setVirtualHost("erp");//虚拟主机 默认是 /
        factory.setPort(5672); //连接端口号
        factory.setHost("192.168.203.130");//服务器地址 默认是localhost
        factory.setUsername("admin");
        factory.setPassword("123456");

        //2.创建连接  资源自动释放（连接、信道 关闭）
        try (Connection conn = factory.newConnection(); Channel channel = conn.createChannel()) {


            //3.声明队列
            String queueName = "order";
            channel.queueDeclare(queueName, true, false, false, null);

            //3.5声明死信队列
            String q2 = "dl-queue";
            channel.queueDeclare(q2, true, false, false, null);
            //定义(死信)交换器
            String exName = "my-dlx";
            channel.exchangeDeclare(exName, BuiltinExchangeType.FANOUT);//路由键

            //绑定死信队列和死信交换器
            channel.queueBind(q2, exName, "");
            //4.发布消息
            /**
             * MessageProperties.PERSISTENT_TEXT_PLAIN消息持久化
             */
            AMQP.BasicProperties properties = new AMQP.BasicProperties()
                    .builder()
                    .expiration("5000")//设置消息过期时间 单位是s
                    .build();
            channel.basicPublish("", queueName, properties, "dd20240001".getBytes());


            System.out.println("消息发布完毕，服务器已收到");
        }

    }
}